#!/usr/bin/env python
# -*- coding: utf8 -*-
# title       :
# description :
# author      :'ShenMeng'
# coding:utf-8

from PySide import QtGui
from PySide import QtCore
import sys
import math



class NoBorderWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(NoBorderWidget, self).__init__(parent)

        ###############################
        ###---initialize variable---###
        ###############################
        self._padding = 5
        self.SHADOW_WIDTH=0   #边框距
        self.isLeftPressDown = False #鼠标左键是否按下
        self.dragPosition=0     #拖动时坐标
        self.Numbers = self.enum(UP=0, DOWN=1, LEFT=2, RIGHT=3, LEFTTOP=4, LEFTBOTTOM=5, RIGHTBOTTOM=6, RIGHTTOP=7, NONE=8) #枚举参数
        #self.setMinimumHeight(100)  #窗体最小高度
        #self.setMinimumWidth(100) #窗体最小宽度
        self.dir=self.Numbers.NONE #初始鼠标状态

        self.setWindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowSystemMenuHint | QtCore.Qt.WindowMinMaxButtonsHint)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
        self.setMouseTracking(True)


    # 枚举参数
    def enum(self, **enums):
        return type('Enum', (), enums)

    def region(self,cursorGlobalPoint):
        # 获取窗体在屏幕上的位置区域，tl为topleft点，rb为rightbottom点
        rect = self.rect()
        tl = self.mapToGlobal(rect.topLeft())
        rb = self.mapToGlobal(rect.bottomRight())
        x = cursorGlobalPoint.x()
        y = cursorGlobalPoint.y()
        if(tl.x() + self._padding >= x and tl.x() <= x and tl.y() + self._padding >= y and tl.y() <= y):
            #左上角
            self.dir = self.Numbers.LEFTTOP
            # 设置鼠标形状
            self.setCursor(QtGui.QCursor(QtCore.Qt.SizeFDiagCursor))
        elif(x >= rb.x() - self._padding and x <= rb.x() and y >= rb.y() - self._padding and y <= rb.y()):
            #右下角
            self.dir = self.Numbers.RIGHTBOTTOM
            self.setCursor(QtGui.QCursor(QtCore.Qt.SizeFDiagCursor))
        elif(x <= tl.x() + self._padding and x >= tl.x() and y >= rb.y() - self._padding and y <= rb.y()):
            # 左下角
            self.dir = self.Numbers.LEFTBOTTOM
            self.setCursor(QtGui.QCursor(QtCore.Qt.SizeBDiagCursor))
        elif(x <= rb.x() and x >= rb.x() - self._padding and y >= tl.y() and y <= tl.y() + self._padding):
            #右上角
            self.dir = self.Numbers.RIGHTTOP
            self.setCursor(QtGui.QCursor(QtCore.Qt.SizeBDiagCursor))
        elif(x <= tl.x() + self._padding and x >= tl.x()):
            #左边
            self.dir = self.Numbers.LEFT
            self.setCursor(QtGui.QCursor(QtCore.Qt.SizeHorCursor))
        elif( x <= rb.x() and x >= rb.x() - self._padding):
            #右边
            self.dir = self.Numbers.RIGHT
            self.setCursor(QtGui.QCursor(QtCore.Qt.SizeHorCursor))
        elif(y >= tl.y() and y <= tl.y() + self._padding):
            #上边
            self.dir = self.Numbers.UP
            self.setCursor(QtGui.QCursor(QtCore.Qt.SizeVerCursor))
        elif(y <= rb.y() and y >= rb.y() - self._padding):
            #下边
            self.dir = self.Numbers.DOWN
            self.setCursor(QtGui.QCursor(QtCore.Qt.SizeVerCursor))
        else:
            #默认
            self.dir = self.Numbers.NONE
            self.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))

    def mouseReleaseEvent(self,event):

        if(event.button() == QtCore.Qt.LeftButton):
            self.isLeftPressDown = False
        if(self.dir != self.Numbers.NONE):
            self.releaseMouse()
            self.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))

    def mousePressEvent(self,event):
        if(event.button()==QtCore.Qt.LeftButton):
            self.isLeftPressDown=True
        if(self.dir != self.Numbers.NONE):
            self.mouseGrabber()
        else:

            self.dragPosition = event.globalPos() - self.frameGeometry().topLeft()

    def mouseMoveEvent(self,event):
        gloPoint = event.globalPos()
        rect = self.rect()
        tl = self.mapToGlobal(rect.topLeft())
        rb = self.mapToGlobal(rect.bottomRight())

        if(not self.isLeftPressDown):
            self.region(gloPoint)
        else:
            if(self.dir != self.Numbers.NONE):
                rmove = QtCore.QRect(tl, rb)
                if(self.dir==self.Numbers.LEFT):
                    if(rb.x() - gloPoint.x() <= self.minimumWidth()):
                        rmove.setX(tl.x())
                    else:
                        rmove.setX(gloPoint.x())
                elif(self.dir==self.Numbers.RIGHT):
                    rmove.setWidth(gloPoint.x() - tl.x())
                elif(self.dir==self.Numbers.UP):
                    if(rb.y() - gloPoint.y() <= self.minimumHeight()):
                        rmove.setY(tl.y())
                    else:
                        rmove.setY(gloPoint.y())
                elif(self.dir==self.Numbers.DOWN):
                    rmove.setHeight(gloPoint.y() - tl.y())
                elif(self.dir==self.Numbers.LEFTTOP):
                    if(rb.x() - gloPoint.x() <= self.minimumWidth()):
                        rmove.setX(tl.x())
                    else:
                        rmove.setX(gloPoint.x())
                    if(rb.y() - gloPoint.y() <= self.minimumHeight()):
                        rmove.setY(tl.y())
                    else:
                        rmove.setY(gloPoint.y())
                elif(self.dir==self.Numbers.RIGHTTOP):
                    rmove.setWidth(gloPoint.x() - tl.x())
                    rmove.setY(gloPoint.y())
                elif(self.dir==self.Numbers.LEFTBOTTOM):
                    rmove.setX(gloPoint.x())
                    rmove.setHeight(gloPoint.y() - tl.y())
                elif(self.dir==self.Numbers.RIGHTBOTTOM):
                    rmove.setWidth(gloPoint.x() - tl.x())
                    rmove.setHeight(gloPoint.y() - tl.y())
                else:
                    pass
                self.setGeometry(rmove)
            else:
                return
                super(NoBorderWidget, self).mouseMoveEvent(event)
                #self.move(event.globalPos() - self.dragPosition)
            event.accept()


    def eventFilter(self, source, event):
        if event.type() == QtCore.QEvent.MouseMove:
            if event.buttons() == QtCore.Qt.NoButton:
                gloPoint = event.globalPos()
                self.region(gloPoint)
            else:
                pass  # do other stuff
        return super(NoBorderWidget, self).eventFilter(source, event)


    def paintEvent(self, event):
        # draw shadow
        # return
        # self.m_shadow = "D:/bb.png"
        #
        # painter = QtGui.QPainter(self)
        # bottom = QtCore.QRect(5, 136, 200, 7)
        # top = QtCore.QRect(5, 0, 200, 3)
        # left = QtCore.QRect(0, 3, 5, 133)
        # right = QtCore.QRect(205, 3, 5, 133)
        # topRight = QtCore.QRect(205, 0, 5, 3)
        # topLeft = QtCore.QRect(0, 0, 5, 3)
        # bottomLeft = QtCore.QRect(0, 136, 5, 7)
        # bottomRight = QtCore.QRect(205, 136, 5, 7)
        # tBottom = QtCore.QRect(5, self.height() - 7, self.width() - 10, 7)
        # tTop = QtCore.QRect(5, 0, self.width() - 10, 3)
        # tLeft = QtCore.QRect(0, 3, 5, self.height() - 10)
        # tRight = QtCore.QRect(self.width() - 5, 3, 5, self.height() - 10)
        # tTopLeft = QtCore.QRect(0, 0, 5, 3)
        # tTopRight = QtCore.QRect(self.width() - 5, 0, 5, 3)
        # tBottomLeft = QtCore.QRect(0, self.height() - 7, 5, 7)
        # tBottomRight = QtCore.QRect(self.width() - 5, self.height() - 7, 5, 7)
        # painter.drawPixmap(tBottom, self.m_shadow, bottom)
        # painter.drawPixmap(tTop, self.m_shadow, top)
        # painter.drawPixmap(tLeft, self.m_shadow, left)
        # painter.drawPixmap(tRight, self.m_shadow, right)
        # painter.drawPixmap(tTopRight, self.m_shadow, topRight)
        # painter.drawPixmap(tTopLeft, self.m_shadow, topLeft)
        # painter.drawPixmap(tBottomLeft, self.m_shadow, bottomLeft)
        # painter.drawPixmap(tBottomRight, self.m_shadow, bottomRight)

        # ###---2---###
        path = QtGui.QPainterPath()
        path.setFillRule(QtCore.Qt.WindingFill)
        path.addRect(5, 5, self.width() - 10, self.height() - 10)

        painter = QtGui.QPainter(self)
        painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
        painter.fillPath(path, QtGui.QBrush(QtGui.QColor(0, 170, 230)))
        color = QtGui.QColor(0, 0, 0, 50)
        for i in range(5):
            path = QtGui.QPainterPath()
            path.setFillRule(QtCore.Qt.WindingFill)
            path.addRect(5 - i, 5 - i, self.width() - (5 - i) * 2, self.height() - (5 - i) * 2)
            color.setAlpha(100 - math.sqrt(i) * 50)
            painter.setPen(color)
            painter.drawPath(path)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    shadow = NoBorderWidget()
    shadow.show()
    app.exec_()